package com.dorukdestan.p2pshare.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;

import com.dorukdestan.p2pshare.logger.Logger;

public class MyIp {
	
	public static ArrayList<InetAddress> get(){
		Logger logger = new Logger(MyIp.class);
		ArrayList<InetAddress> myIp = new ArrayList<InetAddress>();
		
		try {
			for (Enumeration<NetworkInterface> ifaces = 
				NetworkInterface.getNetworkInterfaces();
				ifaces.hasMoreElements(); ){
				NetworkInterface iface = ifaces.nextElement();
				
				for(Enumeration<InetAddress> inetadds = iface.getInetAddresses();
					inetadds.hasMoreElements(); ){
					InetAddress address = inetadds.nextElement();
					myIp.add(address);
				}
			}
		} catch (SocketException e) {
			logger.error("Host ip couldn't detected!");
			logger.error(e);
		}
		
		for (InetAddress inetAddress : myIp) {
			logger.debug("my ip " + inetAddress);
		}
		
		return myIp;
	}
	
}
